package com.udata.server.dao.impl;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.udata.server.dao.UserDao;
import com.udata.server.dao.UserProfileDao;
import com.udata.server.model.User;
import com.udata.server.util.EncodeUtil;
import com.udata.server.util.PMF;
import com.udata.shared.exception.OperationException;
import com.udata.shared.model.UserB;
import com.udata.shared.model.UserProfileB;

public class UserDaoImpl extends UserDao {

  @Override
  public UserB saveUser(UserB userB) throws OperationException {
    try {

      PersistenceManager pm = PMF.get().getPersistenceManager();
      try {
        if (userB.getProfileId() == null) {
          UserProfileB userProfileB = new UserProfileB();
          userProfileB = UserProfileDao.getinstance().saveUserProfile(userProfileB);
          userB.setProfileId(userProfileB.getKey());
        }
        User user = null;
        if (this.getUser(userB.getEmail()) == null) {
          if (userB.getKey() != null) {
            user = pm.getObjectById(User.class, userB.getKey());
            user.update(userB);
          } else {
            user = new User(userB);
            user = pm.makePersistent(user);
          }
          return userB;

        } else {
          return null;
        }
      } finally {

        pm.close();
      }
    } catch (Exception e) {
      throw new OperationException(this.getClass().getCanonicalName() + ".saveUser\n"
          + e.getMessage());
    }
  }

  @Override
  public UserB login(String id, String password) throws OperationException {
    try {
      PersistenceManager pm = PMF.get().getPersistenceManager();
      String passwordEncrypt = EncodeUtil.encodePassword(password);

      Query query = pm.newQuery(User.class);
      query.setFilter("email == '" + id + "' && password=='" + passwordEncrypt + "'");
      List<User> results = (List<User>) query.execute();
      if (results.size() > 0) {
        return results.get(0).getBean();
      } else {
        return null;
      }
    } catch (Exception e) {
      throw new OperationException(this.getClass().getCanonicalName() + ".login\n" + e.getMessage());
    }
  }

  @Override
  public UserB getUser(String email) throws OperationException {
    try {
      PersistenceManager pm = PMF.get().getPersistenceManager();
      Query query = pm.newQuery(User.class);
      query.setFilter("email == '" + email + "'");
      List<User> results = (List<User>) query.execute();
      if (results.size() > 0) {
        return results.get(0).getBean();
      } else {
        return null;
      }
    } catch (Exception e) {
      throw new OperationException(this.getClass().getCanonicalName() + ".getUser\n"
          + e.getMessage());
    }
  }

  @Override
  public UserB registerUser(UserB userB) throws OperationException {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    if (this.getUser(userB.getEmail()) == null) {
      try {
        UserProfileB userProfileB = new UserProfileB();
        userProfileB = UserProfileDao.getinstance().saveUserProfile(userProfileB);
        userB.setProfileId(userProfileB.getKey());

        User user = null;
        String password = userB.getPassword();
        String passwordEncrypt = EncodeUtil.encodePassword(password);
        userB.setPassword(passwordEncrypt);

        user = new User(userB);
        user.setPassword(passwordEncrypt);
        user = pm.makePersistent(user);
        userB = user.getBean();
        userB.setPassword(password);
        return user.getBean();

      } catch (Exception e) {
        throw new OperationException(this.getClass().getCanonicalName() + ".saveUser\n"
            + e.getMessage());
      } finally {

        pm.close();
      }
    } else {
      return null;
    }
  }

}
